//
//  YWDemandDetailViewController.m
//  百布业务
//
//  Created by 陈炳桦 on 15/10/28.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//  需求详情

#import "YWDemandDetailViewController.h"
#import "SDCycleScrollView.h"
#import "YWQRCodeViewController.h"
#import "MLPhotoBrowserViewController.h"

@interface YWDemandDetailViewController ()<SDCycleScrollViewDelegate,MLPhotoBrowserViewControllerDataSource,MLPhotoBrowserViewControllerDelegate>

@property (nonatomic,strong) NSArray *conentArray;

@end

@implementation YWDemandDetailViewController

- (instancetype)init
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

#pragma mark - 懒加载
- (NSArray *)conentArray{
    if (_conentArray == nil) {
        _conentArray = @[@"需求编号",@"类别",@"关键词",@"采购数量",@"是否现货",@"买家留言",@"采购电话",@"发布时间"];
    }
    return _conentArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"需求详情";

}


#pragma mark - UITableViewDataSource
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    // 无限循环图片轮播器
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, BBScreenW, 200) imageURLStringsGroup:self.buyerDemand.urls];
    cycleScrollView.delegate = self;
    cycleScrollView.autoScrollTimeInterval = 3; // 时间间隔3秒
    cycleScrollView.backgroundColor = [UIColor whiteColor];
    
    return cycleScrollView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 200;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.conentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"ID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    cell.textLabel.text = self.conentArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 31)];
    label.numberOfLines = 3;
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentRight;
    label.tag = indexPath.row;
    // 设置文字
    [self setText:label];
    
    // 买家留言
    if (indexPath.row == 5) {
        label.textAlignment = NSTextAlignmentLeft;
        label.frame = CGRectMake(0, 0, 200, 62);
    }

    cell.accessoryView = label;
    
    // 采购电话
    if (indexPath.row == 6) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:@"下载百布易卖可看" forState:UIControlStateNormal];
        [button sizeToFit];
        [button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = button;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 5) {
        return 80;
    }
    
    return 44;
}

- (void)setText:(UILabel *)label{
    switch (label.tag) {
        case 0:
            label.text = self.buyerDemand.ID;   //需求编号
            break;
        case 1:
            if ([self.buyerDemand.sampleTypeId isEqualToString:@"1"]) //类别
                label.text = @"面料";
            else if ([self.buyerDemand.sampleTypeId isEqualToString:@"2"])
                label.text = @"辅料";
            
            break;
        case 2:
            label.text = self.buyerDemand.keyWord;   //关键词
            break;
        case 3:
            label.text = self.buyerDemand.needCount;   //采购数量
            break;
        case 4:
            if ([self.buyerDemand.sampleTypeId isEqualToString:@"1"])   //是否现货
                label.text = @"是";
            else
                label.text = @"否";
            break;
        case 5:
            label.text = self.buyerDemand.sampleDescribe;   //买家留言
            break;
        case 6:
                                                        //采购电话
            break;
        case 7:
            label.text = [self getTime:self.buyerDemand.createTimeFormat];   //发布时间
            break;
        default:
            break;
    }
}

- (void)btnClick{

    YWQRCodeViewController *qrcodeVC = [[YWQRCodeViewController alloc] init];
    [self.navigationController pushViewController:qrcodeVC animated:YES];
}

// 计算时间
- (NSString *)getTime:(NSString *)timeString{
    NSString *day = nil;
    NSString *time = nil;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [formatter dateFromString:timeString];
    
    if ([date isToday]) {   // 今天
        day = @"今天";
    }else if([date isYesterday]){  // 昨天
        day = @"昨天";
    }else{      // 其他 年月日
        formatter.dateFormat = @"yyyy-MM-dd";
        day = [formatter stringFromDate:date];
    }
    
    formatter.dateFormat = @"HH:mm";
    time = [formatter stringFromDate:date];
    
    return [NSString stringWithFormat:@"%@ %@",day,time];
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    // 图片游览器
    MLPhotoBrowserViewController *photoBrowser = [[MLPhotoBrowserViewController alloc] init];
    // 淡入淡出效果
    photoBrowser.status = UIViewAnimationAnimationStatusFade;
    // 数据源/delegate
    photoBrowser.delegate = self;
    photoBrowser.dataSource = self;
    // 当前选中的值
    photoBrowser.currentIndexPath = [NSIndexPath indexPathForItem:indexPath.row inSection:0];
    // 展示控制器
    [photoBrowser show];

}

#pragma mark - <MLPhotoBrowserViewControllerDataSource>
- (NSInteger)photoBrowser:(MLPhotoBrowserViewController *)photoBrowser numberOfItemsInSection:(NSUInteger)section{
    return self.buyerDemand.urls.count;
}

#pragma mark - 每个组展示什么图片,需要包装下MLPhotoBrowserPhoto
- (MLPhotoBrowserPhoto *) photoBrowser:(MLPhotoBrowserViewController *)browser photoAtIndexPath:(NSIndexPath *)indexPath{
    // 包装下imageObj 成 ZLPhotoPickerBrowserPhoto 传给数据源
    MLPhotoBrowserPhoto *photo = [[MLPhotoBrowserPhoto alloc] init];
    photo.photoObj = [self.buyerDemand.urls objectAtIndex:indexPath.row];
    // 缩略图
//    UIButton *btn = self.scrollView.subviews[indexPath.row];
//    photo.thumbImage = btn.imageView.image;
    return photo;
}


@end
